package wang.aweb.childcar.controller.management;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.model.pojo.Device;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.query.DeviceParam;
import wang.aweb.childcar.model.req.AddDeviceReq;
import wang.aweb.childcar.model.req.IdReq;
import wang.aweb.childcar.model.req.UpdateDeviceReq;
import wang.aweb.childcar.model.vo.DeviceVO;
import wang.aweb.childcar.tcpserver.ServerHandler;
import wang.aweb.childcar.service.DeviceService;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RequestMapping("/management/device/")
@RestController
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @GetMapping("getAll")
    public ApiRestResponse getAll(){
        List<DeviceVO> deviceVOList=deviceService.getAll();
        return ApiRestResponse.success(deviceVOList);
    }

    @PostMapping("getDetail")
    public ApiRestResponse getDetail(@RequestBody Map<String,Integer> reqMap){
        DeviceVO deviceVO=deviceService.findVOById(reqMap.get("id"));
        return ApiRestResponse.success(deviceVO);
    }
    @PostMapping("lists")
    public ApiRestResponse getList(@RequestParam Integer page,Integer limit, @RequestBody DeviceParam deviceParam){
        PageInfo pageInfo=deviceService.getList(page,limit,deviceParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }
    @ApiOperation("禁用或启用设备")
    @PostMapping("disableorEnable")
    public ApiRestResponse disableOrEnable(@Valid @RequestBody IdReq idReq){
        deviceService.disableOrEnable(idReq.getId());
        return ApiRestResponse.success();

    }
    @ApiOperation("删除设备")
    @PostMapping("del")
    public ApiRestResponse delete(@Valid @RequestBody IdReq idReq){
        deviceService.delete(idReq.getId());
        return ApiRestResponse.success();
    }
    @ApiOperation("向设备ID发送信息")
    @GetMapping("sendMsgToDeviceId")
    public ApiRestResponse sendMsgToDeviceId(@RequestParam String msg,@RequestParam Integer id){
        //通过ID获取channelId
        Device currentDevice=deviceService.findById(id);
        String channelId=ServerHandler.channelMap.get(currentDevice.getImei());
        this.sendMsgToChannelId(msg,channelId);
        return ApiRestResponse.success();
    }
    private void sendMsgToChannelId(String msg,String channelId) {
        ServerHandler.CHANNEL_MAP.forEach((k,v)->{

            if(v.channel().id().toString().equals(channelId)){
                System.out.println(v.channel().id()+":"+channelId);
                v.writeAndFlush(msg);
            }
            System.out.println(1);
        });
    }
    @ApiOperation("新增设备")
    @PostMapping("add")
    public ApiRestResponse add(@Valid @RequestBody AddDeviceReq addDeviceReq){
        Device device=new Device();
        BeanUtils.copyProperties(addDeviceReq,device);
        device.setAgentId(addDeviceReq.getAgent_id());
        deviceService.add(device);
        return ApiRestResponse.success();
    }
    @PostMapping("getFenceId")
    public ApiRestResponse getFenceId(@RequestBody Map<String,Integer> reqMap){
        Integer fenceId=deviceService.getFenceId(reqMap.get("id"));
        return ApiRestResponse.success(fenceId);
    }
    @PostMapping("update")
    public ApiRestResponse update(@Valid @RequestBody UpdateDeviceReq updateDeviceReq){
        Device device=new Device();
        BeanUtils.copyProperties(updateDeviceReq,device);
        device.setAgentId(updateDeviceReq.getAgent_id());
        deviceService.update(device);
        return ApiRestResponse.success();
    }
}
